package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.WeatherDataBean;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class EcWeatherAdapter extends RecyclerView.Adapter<EcWeatherAdapter.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private WeatherDataBean mData;
    private OnItemClickListener mOnItemClickListener = null;
    private int[] img1 = {
            R.mipmap.img_weather6, R.mipmap.img_weather5,
            R.mipmap.img_weather102, R.mipmap.img_weather10,
            R.mipmap.img_weather11, R.mipmap.img_weather12,
            R.mipmap.img_weather4, R.mipmap.img_weather2,
            R.mipmap.img_weather1, R.mipmap.img_weather3,
            R.mipmap.img_hydro8};

    private int[] img2 = {
            R.mipmap.img_weather6_, R.mipmap.img_weather5_,
            R.mipmap.img_weather102, R.mipmap.img_weather10_,
            R.mipmap.img_weather11_, R.mipmap.img_weather12_,
            R.mipmap.img_weather4_, R.mipmap.img_weather2_,
            R.mipmap.img_weather1_, R.mipmap.img_weather3_,
            R.mipmap.img_hydro8_};

    private boolean isTable = com.mingtu.common.utils.ScreenUtils.isTablet();

    public EcWeatherAdapter(Context context, WeatherDataBean mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_ec, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view, isTable);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (isTable) {
            holder.ivPicture.setBackgroundResource(img2[position]);
        } else {
            holder.ivPicture.setBackgroundResource(img1[position]);
        }
        if (mData != null) {
            WeatherDataBean.DataBean dataBean = mData.getData();
            if (dataBean != null) {
                String time = dataBean.getCollectTime();

                String kqwd1 = dataBean.getKqwd1();//空气温度
                String kqsd1 = dataBean.getKqsd1();//空气湿度
                double trwd1 = dataBean.getTrwd1();//土壤温度
                double trsf1 = dataBean.getTrsf1();//土壤水分
                String fs = dataBean.getFs();//风速
                String fx = dataBean.getFx();//风向
                String yl = dataBean.getYl();//雨量
                String dqy = dataBean.getDqy();//大气压
                String zfs = dataBean.getZfs();//总辐射
                String jfs = dataBean.getJfs();//总辐射
                double ddl = dataBean.getDdl();//导电率
                String pm10 = dataBean.getPm10();//PM10
                String pm = dataBean.getPm();//PM2.5
                String fylz = dataBean.getFylz();//负氧离子
                holder.tvTime.setText(time + "");

                if (StringUtils.isEmpty(pm10)) {
                    pm10 = "0";
                }
                if (StringUtils.isEmpty(fylz)) {
                    fylz = "0";
                }

                if (isTable) {
                    if (position % 2 == 0) {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    } else {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    }
                } else {
                    if (position % 4 == 0 || (position + 1) % 4 == 0) {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.text_color_fba));
                    } else {
                        holder.tvNum.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvUnit.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvName.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                        holder.tvTime.setTextColor(context.getResources().getColor(R.color.colorPrimary));
                    }
                }


                if (position == 8) {
                    holder.tvNum.setText(fs + "");
                    holder.tvUnit.setText("m/s");
                    holder.tvName.setText("风速");
                } else if (position == 7) {
                    holder.tvNum.setText(fx + "°");
                    holder.tvUnit.setText("");
                    holder.tvName.setText("风向");
                } else if (position == 9) {
                    holder.tvNum.setText(yl + "");
                    holder.tvUnit.setText("mm");
                    holder.tvName.setText("雨量");
                } else if (position == 10) {
                    holder.tvNum.setText(fylz + "");
                    holder.tvUnit.setText("个/m³");
                    holder.tvName.setText("负氧离子");
                } else if (position == 6) {
                    holder.tvNum.setText(kqwd1 + "");
                    holder.tvUnit.setText("°C");
                    holder.tvName.setText("大气温度");
                } else if (position == 1) {
                    holder.tvNum.setText(kqsd1 + "");
                    holder.tvUnit.setText("%");
                    holder.tvName.setText("大气湿度");
                } else if (position == 0) {
                    holder.tvNum.setText(dqy + "");
                    holder.tvUnit.setText("kpa");
                    holder.tvName.setText("大气压力");
                } else if (position == 2) {
                    holder.tvNum.setText(jfs + "");
                    holder.tvUnit.setText("w/㎡");
                    holder.tvName.setText("TBQ净辐射");
                } else if (position == 3) {
                    holder.tvNum.setText(zfs + "");
                    holder.tvUnit.setText("w/㎡");
                    holder.tvName.setText("TBQ总辐射");
                } else if (position == 4) {
                    holder.tvNum.setText(pm10 + "");
                    holder.tvUnit.setText("ug/m³");
                    holder.tvName.setText("PM10");
                } else if (position == 5) {
                    holder.tvNum.setText(pm + "");
                    holder.tvUnit.setText("ug/m³");
                    holder.tvName.setText("PM2.5");
                }
            }

        }

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return img1.length;
    }

    public void upData(WeatherDataBean data) {
        mData = data;
        notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        public LinearLayout layout;
        public ImageView ivPicture;
        public TextView tvNum;
        public TextView tvUnit;
        public TextView tvName;
        public TextView tvTime;

        public ActivityHolder(View itemView, boolean isTable) {
            super(itemView);
            convertView = itemView;
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            ivPicture = (ImageView) convertView.findViewById(R.id.iv_picture);
            tvNum = (TextView) convertView.findViewById(R.id.tv_num);
            tvUnit = (TextView) convertView.findViewById(R.id.tv_unit);
            tvName = (TextView) convertView.findViewById(R.id.tv_name);
            tvTime = (TextView) convertView.findViewById(R.id.tv_time);

            ViewGroup.LayoutParams lp = layout.getLayoutParams();
            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行3个，间隙是20dp

            int mWidth = (screenWidth - SizeUtils.dp2px(20)) / 2;
            int mHeight = mWidth * SizeUtils.dp2px(125) / SizeUtils.dp2px(178);
            if (isTable) {
                mWidth = (screenWidth - SizeUtils.dp2px(26)) / 3;
                mHeight = mWidth * SizeUtils.dp2px(181) / SizeUtils.dp2px(258);
            }

            lp.width = mWidth;
            lp.height = mHeight;
            layout.setLayoutParams(lp);
        }
    }

}
